package com.matt.service.impl;

import com.matt.domain.course.*;
import com.matt.domain.courseV2.VendorCourse;
import com.matt.domain.courseV2.VendorCoursePackage;
import com.matt.foundation.DO.ClassesTopicDO;
import com.matt.foundation.DO.course.CoursePlanClassesDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.constant.ObservationConstant;
import com.matt.commons.exception.BusinessException;

import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.query.ClassesCoursePlanQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.ClassesRepository;
import com.matt.repository.base.ClassesCoursePlanRepository;
import com.matt.repository.base.ClassesCourseTopicRepository;
import com.matt.repository.course.*;
import com.matt.repository.courseV2.VendorCoursePackageRepository;
import com.matt.repository.courseV2.VendorCourseRepository;
import com.matt.service.CoursePlanClassesService;
import com.matt.service.CourseTopicService;
import com.matt.service.ObservationNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import static com.matt.foundation.model.ErrorCode.NURSERY_DATE_OUT_LIMIT;
import static com.matt.foundation.model.ErrorCode.TOPIC_NOT_EXIST;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/11/22
 * *******************************
 */
@Service
public class CoursePlanClassesServiceImpl implements CoursePlanClassesService{

    @Autowired
    private CoursePlanClassesRepository coursePlanClassesRepository;

    @Autowired
    private ObservationNewService observationNewService;

    @Autowired
    private CourseTopicRepository courseTopicRepository;

    @Autowired
    private CoursePlanTopicRepository coursePlanTopicRepository;

    @Autowired
    private ClassesCourseTopicRepository classesCourseTopicRepository;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private VendorCourseRepository vendorCourseRepository;

    @Autowired
    private VendorCoursePackageRepository vendorCoursePackageRepository;

    @Autowired
    private ClassesCoursePlanRepository classesCoursePlanRepository;

    @Transactional
    @Override
    public CoursePlanClasses createEarly(String date, String beginTime, String endTime, String classesId, String courseId, String areaId) {
        Date begin = DateUtil.parseToDateHour(date + " " + beginTime);
        Date end = DateUtil.parseToDateHour(date + " " + endTime);
        Date day = DateUtil.parseToDate(date);

        List<CoursePlanClasses> oldClassesPlans = coursePlanClassesRepository.findByClassesIdAndCourseDateAndCategory(classesId, day, CourseConstant.Category.EARLY.codeOf());
        for (CoursePlanClasses p : oldClassesPlans) {
            if (DateUtil.hasTimeUnionSet(begin, end, p.getBeginTime(), p.getEndTime()))
                throw new BusinessException(ErrorCode.SAME_TIME_TEACHING_AREA_DUPLICATE, "同一时间段，班级课程不能重复");
        }
        VendorCourse course = vendorCourseRepository.getOne(courseId);

//        VendorCoursePackage vendorCoursePackage = vendorCoursePackageRepository.getOne(course.getCoursePackageId());
        //初始化课程
        CoursePlanClasses planClasses =  CoursePlanClasses.ofEarly(begin, end, day, course.getType(), courseId, classesId, areaId);
        planClasses.setCategory(CourseConstant.Category.EARLY.codeOf());
        //设置课程名称和课程类型
        planClasses.setCourseName(course.getTitle());
        planClasses.setTypeName(CourseConstant.Category.EARLY.valueOf() );
        coursePlanClassesRepository.save(planClasses);

        //保存早教观测点
        // saveObservation(course, planClasses);

        return planClasses;
    }

    @Transactional
    @Override
    public Boolean createNursery(String topicId, Integer courseType, String date, String beginTime, String endTime, String courseId, String areaId) {
        CourseTopic topic = courseTopicRepository.getOne(topicId);
        if (Objects.isNull(topic))
            throw new BusinessException(TOPIC_NOT_EXIST, "主题课程不存在");
        Date begin = DateUtil.parseToDateHour(date + " " + beginTime);
        Date end = DateUtil.parseToDateHour(date + " " + endTime);
        Date day = DateUtil.parseToDate(date);

        if (day.compareTo(topic.getBeginDate()) < 0 || day.compareTo(topic.getEndDate()) > 0)
            throw new BusinessException(NURSERY_DATE_OUT_LIMIT, "托班课程超出主题课程时间范围");

        List<CoursePlanTopic> oldCourseNurseryList = coursePlanTopicRepository.findByTopicIdAndCourseDate(topicId, day);
        for (CoursePlanTopic p : oldCourseNurseryList) {
            if (DateUtil.hasTimeUnionSet(begin, end, p.getBeginTime(), p.getEndTime()))
                throw new BusinessException(ErrorCode.SAME_TIME_TEACHING_AREA_DUPLICATE, "同一时间段，主题课程不能重复");
        }


        CoursePlanTopic coursePlanTopic = new CoursePlanTopic();
        coursePlanTopic.setBeginTime(begin);
        coursePlanTopic.setEndTime(end);
        coursePlanTopic.setCourseDate(day);
        coursePlanTopic.setCourseType(courseType);
        coursePlanTopic.setTopicId(topicId);
        VendorCourse course = vendorCourseRepository.getOne(courseId);
        coursePlanTopic.setCourseName(course.getTitle());
        coursePlanTopic.setTypeName(CourseConstant.Category.NURSERY.valueOf() );
        coursePlanTopic.setCourseId(courseId);
        coursePlanTopicRepository.save(coursePlanTopic);

        List<ClassesTopicDO> classesList = classesCourseTopicRepository.findByTopic(topicId);


        for (ClassesTopicDO c : classesList) {
            CoursePlanClasses planClasses = CoursePlanClasses.ofTopic(coursePlanTopic, areaId, c.getClassesId());
            planClasses.setAreaId(c.getAreaId());
            planClasses.setClassesId(c.getClassesId());


            coursePlanClassesRepository.save(planClasses);

        }
        return true;
    }

    @Override
    public CoursePlanClasses getOne(String id) {
        return coursePlanClassesRepository.getOne(id);
    }

    @Override
    public  List<CoursePlanClassesDO> getMyWeekCourseList(ClassesCoursePlanQuery query) {

        List<CoursePlanClassesDO> coursePlanClassesList = classesCoursePlanRepository.getMyWeekCourseList(query);
        return coursePlanClassesList;
    }


}
